package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.qianyue.PayActivity;
import com.itboye.ihomebank.bean.RentingBillBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2018/3/12 0012.
 */

public class RentingBillAdapter extends RecyclerView.Adapter<RentingBillAdapter.BillViewHolder> {
    private Context context;
    private ArrayList<RentingBillBean.Bill> list;
    private LayoutInflater inflater;
    public RentingBillAdapter(Context context, ArrayList<RentingBillBean.Bill> list){
      this.context=context;
      this.list=list;
      this.inflater=LayoutInflater.from(context);

    }
    @Override
    public BillViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new BillViewHolder(inflater.inflate(R.layout.item_rentingbill,parent,false));
    }

    @Override
    public void onBindViewHolder(BillViewHolder holder, final int position) {
        if (list.size()>0){
            holder.titleText.setText(list.get(position).getBill_name());
            final float a=list.get(position).getBill_amount()/100;
            holder.priceText.setText(a+"");
            holder.contractText.setText("合同号:"+list.get(position).getContract_no());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");

            holder.dateText.setText(simpleDateFormat.format(new Date(list.get(position).getCreate_time())));
            if (list.get(position).getBill_status() ==0){
                holder.payBtn.setText("付款");
                holder.payBtn.setEnabled(true);
                holder.payBtn.setBackground(context.getResources().getDrawable(R.drawable.shape_corner_blue));
                holder.payBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, PayActivity.class);
                        intent.putExtra("time",list.get(position).getCreate_time());
                        intent.putExtra("money",a+"元" );
                        intent.putExtra("contract_no",list.get(position).getContract_no());
                        intent.putExtra("itemid", list.get(position).getId()+"");
                        intent.putExtra("pay_code_type", "house_bill");
                        context.startActivity(intent);
                    }
                });
            }else {
                holder.payBtn.setText("已付款");
                holder.payBtn.setEnabled(false);
                holder.payBtn.setBackground(context.getResources().getDrawable(R.drawable.shape_corner_gray));
            }


        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class BillViewHolder extends RecyclerView.ViewHolder{
        TextView titleText,priceText,contractText,dateText,payBtn;

        public BillViewHolder(View itemView) {
            super(itemView);
            titleText= (TextView) itemView.findViewById(R.id.titleText);
            priceText= (TextView) itemView.findViewById(R.id.priceText);
            contractText= (TextView) itemView.findViewById(R.id.contractText);
            dateText= (TextView) itemView.findViewById(R.id.dateText);
            payBtn= (TextView) itemView.findViewById(R.id.payBtn);
        }
    }
}
